//
//  ActivityDetailViewController.m
//  豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "ActivityDetailView.h"
#import "UIImageView+WebCache.h"
#import "DataBaseSingleton.h"
#import "LoginViewController.h"
@interface ActivityDetailViewController ()

@property (nonatomic, retain)ActivityDetailView *activityDV;

@end

@implementation ActivityDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    scroll.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:scroll];
    
    [scroll release];
    
    self.activityDV = [[ActivityDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.activityDV.backgroundColor = [UIColor whiteColor];
    
    [scroll addSubview:self.activityDV];
    
    [self.activityDV release];
    
    self.navigationItem.title = self.act.title;

    self.activityDV.nameLabel.text = self.act.title;
    
    self.activityDV.kindLabel.text = self.act.category_name;

    self.activityDV.timeLabel.text = self.act.begin_time;
    
    self.activityDV.sponsorLabel.text = self.act.name;
    
    self.activityDV.addressLabel.text = self.act.address;
    
    self.activityDV.contentLabel.text = self.act.content;
    
    NSURL *url = [NSURL URLWithString:self.act.image];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    
    [self.activityDV.cImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage.png"]];
    
    [self.activityDV.contentLabel sizeToFit];
    
    CGFloat height = self.activityDV.contentLabel.frame.origin.y + self.activityDV.contentLabel.frame.size.height + 64;
    
    [scroll setContentSize:CGSizeMake(375, height)];
    
}

- (void)buttonAction:(UIBarButtonItem *)btn
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"islogin"] isEqualToString:@"0"]) {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *naC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        
        [self presentViewController:naC animated:YES completion:^{
            
            
        }];
        
    }else{
        NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
        NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectMovieName:name];
        
        if ([array containsObject:self.act.title]) {
            UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已收藏过!" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:alertCon animated:YES completion:^{
                
            }];
            UIAlertAction *alertActCn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }];
            [alertCon addAction:alertActCn];
        }
        else{
            [[DataBaseSingleton shareDataBase] insertCollectList:self.act.title :name];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
